﻿package com.emr.system.action;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray; 
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * ACTION基础类，集成一些常用的方法
 */
public abstract class AbstractBaseAction extends ActionSupport{

	private static final long serialVersionUID = -2177123710563883457L;
	private String message;//提示信息
	private Map<String,Object> logInfo = new HashMap<String,Object>();	//日志信息。最终将以XML形式展现给用户。
	private int start = 0;//接收分页开始记录位置
	private int limit = 0;//单页显示数量
	/**
	 * 将对象转化成json格式数据
	 * @param obj
	 * @return   
	 */
	protected String ChangeToJson(Object obj){
		if (obj == null)
			return "";
		JSONArray jsonObject = JSONArray.fromObject(obj);
		return jsonObject.toString();
	}
	 
	/**
	 * 将数据以html方式输出到页面
	 * @param String
	 */
	protected void outputHTMLData(String outputString){ 
		try
		{ 
			this.getResponse().setContentType("text/html");
			this.getResponse().setCharacterEncoding("utf-8");
			this.getResponse().setHeader("Cache-Control","no-cache");
			this.getResponse().getWriter().println(outputString);
			this.getResponse().getWriter().close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}

	/**
	 * 将数据以ajax方式输出到页面
	 * @param String
	 */
	protected void outputAjaxJsonData(String outputString){ 
		try
		{ 
			this.getResponse().setContentType("text/json");
			this.getResponse().setCharacterEncoding("utf-8");
			this.getResponse().setHeader("Cache-Control","no-cache");
			this.getResponse().getWriter().println(outputString);
			this.getResponse().getWriter().close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	/**
	 * 将对象转化成json格式数据以ajax方式输出到页面
	 * @param Object
	 */
	protected void outputAjaxJsonData(Object obj){
		String json = "";
		if (obj == null){
			json = "{success:false}";
			return;
		}
		else{			
			JSONArray jsonObject = JSONArray.fromObject(obj);
			try
			{
				json = jsonObject.toString();
				this.getResponse().setContentType("text/json");
				this.getResponse().setCharacterEncoding("utf-8");
				this.getResponse().setHeader("Cache-Control","no-cache");
				this.getResponse().getWriter().println(json);
				this.getResponse().getWriter().close();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取url参数的值
	 * @param parameter:参数名称
	 * @return
	 */
	protected String getParameter(String parameter){
		return this.getRequest().getParameter(parameter);
	}
	/**
	 * <p>取得request对象</p>
	 * @return
	 */
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	/**
	 * <p>取得response对象</p>
	 * @return
	 */
	protected HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	/**
	 * <p>取得session对象</p>
	 * @return
	 */
	protected HttpSession getSession(){
		return this.getRequest().getSession();
	}
	/**
	 * <p>将对象保存到session中</p>
	 * @param key
	 * @param obj
	 */
	protected void setSessionAttribute(String key,Object obj){
		if (key != null && !key.trim().equals(""))
			this.getSession().setAttribute(key, obj);
	}
	/**
	 * <p>获取session中对应key的对象</p>
	 * @param key
	 * @return
	 */
	protected Object getSessionAttribute(String key){	
		if (key != null && !key.trim().equals(""))
			return this.getSession().getAttribute(key);
		else 
			return null;
	}
	/**
	 * <p>移除session中对应key的对象</p>
	 * @param key
	 * @return
	 */
	protected void removeSessionAttribute(String key){
		if (key != null && !key.trim().equals(""))
			this.getSession().removeAttribute(key);
	}
	
	/**
	 * 获取提示信息
	 * @return
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * 设置提示信息
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	
	public Map<String, Object> getLogInfo() {
		return logInfo;
	}

	public void setLogInfo(Map<String, Object> logInfo) {
		this.logInfo = logInfo;
	}

	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
}
